package com.wxg.tools.cmd;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;

public class CmdRuntimeExecTest {

	@Test
	//@Ignore
	public void test006() throws IOException{
		JavaRuntimeExec jre = new JavaRuntimeExec();
		String[] commands = "cmd /c netstat -aon | findstr 5000".split(" ");
		String dir = System.getProperty("user.dir");
		jre.runtimeExec(commands, dir);
		BufferedReader br = jre.fetchReader();
		String line = null;
		List<String> list = new ArrayList<String>();
		while((line=br.readLine())!=null){
			list.add(line);
		}
		for(String str : list){
			System.out.println( str );
		}
		jre.destroy();
	}
	
	@Test
	@Ignore
	public void test005() throws IOException{
		CmdRuntimeExec cre = new CmdRuntimeExec();
		String[] commands = "netstat -aon | findstr 5000".split(" ");
		String dir = System.getProperty("user.dir");
		cre.runtimeExec(commands, dir);
		
//		try {
//			Thread.currentThread().sleep(60*1000);
//		} catch (InterruptedException e) {
//			e.printStackTrace();
//		}
//		
//		
//		BufferedWriter bw = null;
//		OutputStream os = process.getOutputStream();
//		bw = new BufferedWriter(new OutputStreamWriter(os));
//		bw.write("ctrl+c\n");
//		bw.close();
	}
	
	@SuppressWarnings("static-access")
	@Test
	@Ignore
	@Deprecated
	public void test004(){
		CmdRuntimeExec cre = new CmdRuntimeExec();
		String[] commands = "cmd /c hexo server -p 5000".split(" ");
		String dir = "C:/wxg2/firstblog";
		Process process = cre.runtimeExec(commands, dir);
		
		try {
			Thread.currentThread().sleep(60*1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.printf("process.isAlive() : %b\n", process.isAlive());
		if(process.isAlive()){
			process.destroy();
			System.out.printf("process.isAlive() : %b\n", process.isAlive());
		}
	}
	
	@SuppressWarnings("static-access")
	@Test
	@Ignore
	public void test003(){
		CmdRuntimeExec cre = new CmdRuntimeExec();
		String[] commands = "cmd /c echo %cd%".split(" ");
		String dir = System.getProperty("user.dir");
		Process process = cre.runtimeExec(commands, dir);
		try {
			Thread.currentThread().sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.printf("isAlive : %b\n", process.isAlive());
		Assert.assertEquals(false, process.isAlive());
	}
	
	@Test
	@Ignore
	public void test002(){
		CmdRuntimeExec cre = new CmdRuntimeExec();
		String[] commands = "cmd /c echo %cd%".split(" ");
		String dir = System.getProperty("user.dir");
		Process process = cre.runtimeExec(commands, dir);
		boolean isAlive = process.isAlive();
//		System.out.printf("isAlive : %b\n", isAlive);
		Assert.assertEquals(false, isAlive);
	}
	
	@Test
	@Ignore
	public void test001(){
		String javaHome = System.getenv("JAVA_HOME");
		System.out.println(javaHome);
	}
}
